package com.szxx.suit;

import com.szxx.constans.PokerGameConstant;
import com.szxx.domain.Card;
import com.szxx.domain.Cards;
import com.szxx.domain.DdzCard;

/**
 * 斗地主分析结果 <br>
 *
 * @author Ambrose
 */
public class DdzAnalysisResult extends AbstractAnalysisResult {
	
	/** 缓存类型 */
	private AbstractSuitType type;
	/** 分析结果的代表权值，最终比较到这个权值 */
	private int weight = -1;
	/** 点数计数器 */
	private final AnalyzerCounter allValueCounter = new AnalyzerCounter();
	/** 所有牌数目 */
	private int cardNum = 0;
	/** 非癞子点数计数器 */
	private final AnalyzerCounter noneLaziValueCounter = new AnalyzerCounter();
	/** 王牌数量 */
	private int jokerCardNum = 0;
	/** 癞子点数计数器 */
	private final AnalyzerCounter laziValueCounter = new AnalyzerCounter();
	/** 未被赋值的癞子数量 */
	private int laziCardNum = 0;
	/** 已经被赋值的癞子数量 */
	private int changedLaziCardNum = 0;
	/** 排序 比如44445555 怎么排序 */
	private int sortFirstValue = -1;
	
	@Override
	public AbstractSuitType getType() {
		return type;
	}

	@Override
	protected int doCompareTo(AbstractAnalysisResult compareTo) { // 上层已经判定了牌型大小
		if (cardNum != ((DdzAnalysisResult)compareTo).cardNum) { // 长度不一致
			return -1;
		} else {
			return weight - ((DdzAnalysisResult)compareTo).weight;
		}
	}
	
	@Override
	public DdzAnalysisResult clone() {
		DdzAnalysisResult copy = new DdzAnalysisResult();
		copy.setType(type);
		copy.setWeight(weight);
		copy.setCardNum(cardNum);
		copy.setLaziCardNum(laziCardNum);
		copy.setChangedLaziCardNum(changedLaziCardNum);
		copy.setJokerCardNum(jokerCardNum);
		copy.setSortFirstValue(sortFirstValue);
		copy.getAllValueCounter().getCounter().putAll(allValueCounter.getCounter());
		copy.getLaziValueCounter().getCounter().putAll(laziValueCounter.getCounter());
		copy.getNoneLaziValueCounter().getCounter().putAll(noneLaziValueCounter.getCounter());
		return copy;
	}
	
	/**
	 * 收集牌信息
	 * 
	 * @param cards
	 * @param term
	 * @param result
	 */
	public final void collectNumber(Cards cards, AbstractSuitTerm term) {
		this.allValueCounter.clear();
		this.cardNum = 0;
		this.noneLaziValueCounter.clear();
		this.jokerCardNum = 0;
		this.laziValueCounter.clear();
		this.laziCardNum = 0;
		this.changedLaziCardNum = 0;
		for (Card card : cards) {
			if (card != null) {
				cardNum++;
				this.getAllValueCounter().addValueCount(card.getValue());
				if (card.getValue() == PokerGameConstant.RED_JOKER_SHAPE_NUM
						|| card.getValue() == PokerGameConstant.BLACK_JOKER_SHAPE_NUM) {
					jokerCardNum++;
				}
				DdzCard ddzCard = (DdzCard) card;
				if (ddzCard.hasNotChanged()) { // 如果是癞子没有赋值
					this.getLaziValueCounter().addValueCount(ddzCard.getValue());
					laziCardNum++;
				} else if (ddzCard.hasChanged()) { // 如果是癞子且已经赋值
					this.getNoneLaziValueCounter().addValueCount(ddzCard.getValue());
					changedLaziCardNum++;
				} else { // 不是癞子
					this.getNoneLaziValueCounter().addValueCount(ddzCard.getValue());
				}
			}
		}
	}
	
	/**
	 * 是否所有牌都是癞子
	 * 
	 * @return
	 */
	public boolean isAllCardLazi() {
//		return getNoneCardNum() == 0;
		return laziCardNum + changedLaziCardNum == cardNum;
	}
	
	/**
	 * 非癞子牌数量（包括已经赋值的了的癞子）
	 * 
	 * @return
	 */
	public int getNoneCardNum() {
		return cardNum - laziCardNum;
	}
	
	@Override
	public String toString() {
		return "DdzAnalysisResult [type=" + type + ", weight=" + weight + "]";
	}

	// TODO ----------------- setter & getter ---------------
	public AnalyzerCounter getAllValueCounter() {
		return allValueCounter;
	}
	
	public AnalyzerCounter getNoneLaziValueCounter() {
		return noneLaziValueCounter;
	}

	public AnalyzerCounter getLaziValueCounter() {
		return laziValueCounter;
	}
	
	public void setType(AbstractSuitType type) {
		this.type = type;
	}

	public int getWeight() {
		return weight;
	}
	
	public void setWeight(int weight) {
		this.weight = weight;
	}

	public int getCardNum() {
		return cardNum;
	}

	public void setCardNum(int cardNum) {
		this.cardNum = cardNum;
	}

	public int getJokerCardNum() {
		return jokerCardNum;
	}

	public void setJokerCardNum(int jokerCardNum) {
		this.jokerCardNum = jokerCardNum;
	}

	public int getLaziCardNum() {
		return laziCardNum;
	}

	public void setLaziCardNum(int laziCardNum) {
		this.laziCardNum = laziCardNum;
	}

	public int getChangedLaziCardNum() {
		return changedLaziCardNum;
	}

	public void setChangedLaziCardNum(int changedLaziCardNum) {
		this.changedLaziCardNum = changedLaziCardNum;
	}

	public int getSortFirstValue() {
		return sortFirstValue;
	}

	public void setSortFirstValue(int sortFirstValue) {
		this.sortFirstValue = sortFirstValue;
	}
	
}
